<?php

/**
 * Subclass for performing query and update operations on the 'statement_answer' table.
 *
 *
 *
 * @package lib.model
 */
class StatementAnswerPeer extends BaseStatementAnswerPeer
{

	const ANSWER_AGREE = "a";
	const ANSWER_NOANSWER = "b";
	const ANSWER_DISAGREE = "c";

	static public function getValidAnswers(){
		return array(
		self::ANSWER_AGREE,
		self::ANSWER_DISAGREE,
		self::ANSWER_NOANSWER
		);
	}

	static public function saveAnswer($statement_id,$answer,User $user){

		if(!in_array($answer,self::getValidAnswers())) throw new sfException('Invalid answer!');

		//Alle voorgaande antwoorden op deze statement van deze gebruiker weghalen!
		$c = new Criteria();
		$c->add(StatementAnswerPeer::USER_ID,$user->getId());
		$c->add(StatementAnswerPeer::STATEMENT_ID,$statement_id);
		StatementAnswerPeer::doDelete($c);

		$statement = LypoStatementPeer::retrieveByPK($statement_id);
		$statement_answer = new StatementAnswer();
		$statement_answer->setStatementId($statement_id);
		$statement_answer->setUser($user);
		$statement_answer->setAnswer($answer);
		$statement_answer->save();

		UserPeer::updateLypoSignatureForUser($user);

		return true;
	}

	//TODO: Anders oplossen, dit is te foutgevoelig, omdat dit nu elke keer handmatig moet worden opgeroepen bij elke verandering van signature
	static public function createUsersAnswersFromLypoSignature($user_id, $signature){
		$sig = new LypoSignature($signature);
		if($values = $sig->getValuePairs()){
			//Oude waarden wissen!
			$c = new Criteria();
			$c->add(StatementAnswerPeer::USER_ID,$user_id);
			StatementAnswerPeer::doDelete($c);

			//Nieuwe antwoorden toevoegen
			foreach($values as $statement_id => $answer){
				$sa = new StatementAnswer();
				$sa->setUserId($user_id);
				$sa->setStatementId($statement_id);
				$sa->setAnswer($answer);
				$sa->save();
			}
			return true;
		}
		return false;
	}


	static public function getUserRegistrationAnswersPager($user,$page=1,$max_per_page=5){
		//Haal de registratie criteria op en selecteer de juiste pagina met statements ...
		$c = LypoStatementPeer::getRegistrationStatementsCriteria();
		$c->add(StatementAnswerPeer::USER_ID,$user->getId());
		$c->addJoin(StatementAnswerPeer::STATEMENT_ID,LypoStatementPeer::ID);

		$pager = new sfPropelPager('StatementAnswer',$max_per_page);
		$pager->setCriteria($c);
		$pager->setPeerMethod('doSelectJoinLypoStatement');
		$pager->setPage($page);
		$pager->init();
		return $pager;
	}


	static public function getStatementAnswerForUser(LypoStatement $statement, User $user){
		$c = new Criteria();
		$c->add(StatementAnswerPeer::USER_ID,$user->getId());
		$c->add(StatementAnswerPeer::STATEMENT_ID,$statement->getId());
		return StatementAnswerPeer::doSelectOne($c);
	}

	/*
	 * Geeft een op statement_id geindexeerde array terug ...
	 */
	static public function getUserAnswersIds(User $user){
		$c = new Criteria();
		$c->add(StatementAnswerPeer::USER_ID,$user->getId());
		$ret = StatementAnswerPeer::doSelect($c);
		$answers = array();
		if($ret){
			foreach($ret as $answer){
				$answers[$answer->getStatementId()]=$answer->getAnswer();
			}
		}
		return $answers;
	}
}

